package com.offcn.service;

import com.offcn.dao.UserDao;
import com.offcn.dao.UserDaoImpl;
import com.offcn.entity.User;
import com.offcn.utlis.PageUtils;

import java.util.List;

public class UserServiceImpl implements UserService {
    //调用dao层
    UserDao userDao = new UserDaoImpl();

    /**
     * 查询用户
     * @param username
     * @param password
     * @return
     */
    @Override
    public User loginUser(String username, String password) {
        return userDao.loginUser(username,password);
    }

    /**
     *
     * @param user
     * @return
     */
    @Override
    public boolean add(User user) {
        return userDao.add(user)>0;
    }

    /**
     * 分页查询
     * @param search
     * @param pageSize
     * @param currentPage
     * @return
     */
    @Override
    public PageUtils<User> findByAll(String search, int pageSize, int currentPage) {
        // 查询总记录
               StringBuffer stringBuffer = new StringBuffer("select * from user where name like ? ");
        int size = userDao.find(stringBuffer.toString(), "%" + search + "%").size();
       // 分页
        stringBuffer.append(" limit  ? , ? ");
        List<User> userList = userDao.find(stringBuffer.toString(), "%" + search + "%", (currentPage - 1) * pageSize, pageSize);
        PageUtils<User> pageUtils = new PageUtils<User>(pageSize,currentPage,size,userList);
        return pageUtils;
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @Override
    public boolean updateUser(User user) {
        return userDao.updateUser(user)>0;
    }

    @Override
    public boolean del(String[] ids) {
        try {
            for (String uid : ids) {
               int k  = userDao.deleteByUid(uid);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        return true;
    }


}
